package com.gree.pool;

import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.ThreadPoolExecutor;

import org.apache.ibatis.io.Resources;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.gree.pool.file.ReadFile;

public class ThreadPool {
	static ThreadPoolTaskExecutor executor = null;
	static {
		try {
			Properties props = Resources.getResourceAsProperties("pool.properties");
			Integer core_pool_size = Integer.parseInt(props.getProperty("task.core_pool_size"));
			Integer max_pool_size = Integer.parseInt(props.getProperty("task.max_pool_size"));
			Integer queue_capacity = Integer.parseInt(props.getProperty("task.queue_capacity"));
			Integer keep_seconds = Integer.parseInt(props.getProperty("task.keep_alive_seconds"));
			executor = new ThreadPoolTaskExecutor();
			executor.setCorePoolSize(core_pool_size);
			executor.setMaxPoolSize(max_pool_size);
			executor.setKeepAliveSeconds(keep_seconds);
			executor.setQueueCapacity(queue_capacity);
			executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
			executor.initialize();
		} catch (IOException e) {
			e.getMessage();
		}

	}

	public static void testNoThreadTask(String path, String loginId,String userId, String infoId, String companyId, String fileName,Integer rowLines) {
		executor.execute(new ReadFile(path, loginId, userId, infoId, companyId, fileName, rowLines));
	}
	 

	public static int getActiveCount() {
		return executor.getActiveCount();
	}

	public static int getCorePoolSize() {
		return executor.getCorePoolSize();
	}

	public static int getSize() {
		return getCorePoolSize() - getActiveCount();
	}

}
